<?php
namespace Admin\Controller;
use Think\Controller;
class OrderController extends Controller {
    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Admin_autoload();
        //当前页仅管理员以上可以访问 测试好后客服等也可访问
        /*if($this->admin_info['admin_group_id']>2){
            echo "<script>alert('当前页面仅管理员以上可以访问，请您使用管理员以上账号登录');history.back(-1);</script>";exit;
        }*/
    }
    //
    public function index(){
        $data['title']='实物订单 交易 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Order");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $order_list=$Model->get_order_list();
        //print_r($order_list);exit;
        $order_total=$Model->get_order_total();
        //echo $user_total;exit;
        $page_total=ceil($order_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['order_total']=$order_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("order_list", $order_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function order_edit(){
        $data['title']='编辑订单 交易 - 找金银网';
        $order_id=I('path.2');
        $Model=D("Order");
        $Model->order_id=$order_id;
        $order_info=$Model->get_order_info();
        //print_r($order_info);exit;
        $this->assign("data", $data);
        $this->assign("order_info", $order_info);
        $this->display();
    }
    //
    public function order_edit_post(){
        if($_POST['order_amount']!=''){
            $post=I('post.');
            //print_r($post);exit;
            $Model=D("Order");
            $Model->post=$post;
            if(!$Model->order_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/order/index';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //更新排序及批量删除
    public function update_order()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Order");
            $Model->ids=$ids;
            if(!$Model->delete_order()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }
        /*elseif(I('post.product_ids')){
            $ids=I('post.product_ids');
            $sorts=I('post.sorts');
            $Model=D("Product");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_product_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }*/
    }
    //
    public function match(){
        $data['title']='撮合管理 交易 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Match");
        //$MatchStatus=D("MatchStatus");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $match_list=$Model->get_match_list();
        //print_r($match_list);exit;
        $match_total=$Model->get_match_total();
        //echo $user_total;exit;
        $page_total=ceil($match_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['match_total']=$match_total;
        $pager['page_total']=$page_total;
        //$match_status_list=$MatchStatus->get_match_status_list();
        //print_r($match_status_list);exit;
        //$cat_list=$Model->get_cat_list();
        $this->assign("match_list", $match_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //更新排序及批量删除
    public function update_match()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Match");
            $Model->ids=$ids;
            if(!$Model->delete_match()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.match_ids')){
            $ids=I('post.match_ids');
            $sorts=I('post.sorts');
            $Model=D("Match");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_match_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //意向批量删除
    public function update_intention()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Intention");
            $Model->ids=$ids;
            if(!$Model->delete_intention()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }
    }
    //
    public function match_edit(){
        $data['title']='编辑撮合信息 交易 - 找金银网';
        $match_id=I('path.2');
        //echo $match_id;exit;
        $Model=D("Match");
        $MatchState=D("MatchState");
        $Model->match_id=$match_id;
        $match_info=$Model->get_match_info();
        $match_state_list=$MatchState->get_match_state_list();
        //print_r($match_info);exit;
        $this->assign("data", $data);
        $this->assign("match_info", $match_info);
        $this->assign("match_state_list", $match_state_list);

        $Admin=D("Admin");  //后台用户
        $admin_list=$Admin->get_admin_list();
        $this->assign("admin_list", $admin_list);

        $this->display();
    }
    //
    public function match_edit_post(){
        if($_POST['match_desc']==''){
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
        $post=I('post.');
//        dump(intval($post['goods_amount']));exit;
        $post['match_desc']=I('post.match_desc','','');
        //print_r($post);exit;
        $Model=D("Match");
        $Order=D("Order");
        $Address=M("Member_address");
        $Morder=M("Order");
        $Purchase=M("Purchase");
        $member_id=$Purchase->where('purchase_id='.$post['purchase_id'])->getField('member_id');
        $match_id=$post['match_id'];
        $Model->post=$post;
        if(!$Model->match_edit_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }

        //print_r($post);exit;
        //根据需要写入订单 3为已指定 以上即可自动生成订单
        if($post['match_state_id']>=3){
            $data['order_sn']=date('YmdHis').rand(001,999);
            $data['match_id']=$post['match_id'];
            $data['seller_id']=$post['seller_id'];
            $data['buyer_id']=$post['buyer_id'];
            $data['order_amount']=$post['goods_amount'];
            $data['order_type']=2;  //1为商品订单2为撮合订单
            $data['order_state_id']=1;  //状态ID
            $data['order_time']=time();
            if(!$Morder->where('match_id='.$match_id)->getField('shipping_address')){
                $data['shipping_address']=$Address->where("member_id=$member_id", "default=1")->getField('member_address');
                $data['shipping_zipcode']=$Address->where("member_id=$member_id", "default=1")->getField('member_zipcode');
                $data['shipping_truename']=$Address->where("member_id=$member_id", "default=1")->getField('member_truename');
                $data['shipping_telephone']=$Address->where("member_id=$member_id", "default=1")->getField('member_telephone');
            }
            //print_r($data);exit;
            $Order->data=$data;
            if(!$Order->check_add_order()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
        }
        echo "<script>alert('提交成功');window.location.href='/order/match';</script>";
    }
    //
    public function match_add(){
        $data['title']='添加撮合信息 交易 - 找金银网';
        $Purchase=D("Purchase");  //采购
        $Supply=D("Supply");  //供货
        $MatchState=D("MatchState");  //供货
        $purchase_member_list=$Purchase->get_purchase_member_list();
        $supply_member_list=$Supply->get_supply_member_list();
        $match_state_list=$MatchState->get_match_state_list();
        //print_r($match_status_list);exit;
        $this->assign("data", $data);
        $this->assign("purchase_member_list", $purchase_member_list);
        $this->assign("supply_member_list", $supply_member_list);
        $this->assign("match_state_list", $match_state_list);

        $Admin=D("Admin");  //后台用户
        $admin_list=$Admin->get_admin_list();
        $this->assign("admin_list", $admin_list);

        $this->display();
    }
    //
    public function match_add_post(){
        if($_POST['match_desc']!=''){
            $post=I('post.');
            $post['match_desc']=I('post.match_desc','','');
            $post['match_time']=time();
            $post['match_sn']=date('YmdHis',time()).rand(001,999);
            //print_r($post);exit;
            $Model=D("Match");
            $Morder=M("Order");
            $Address=M("Member_address");
            $Purchase=M("Purchase");
            $member_id=$Purchase->where('purchase_id='.$post['purchase_id'])->getField('member_id');
            $Model->post=$post;
            if(!$Model->match_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            $match_id=$Model->where("match_sn=".$post['match_sn'])->getField('match_id');
            //根据需要写入订单 3为撮合意向成功，待付款 先检测是否存在，如果不存在则写入
            if($post['match_state_id']>=3){
                $data['order_sn']=date('YmdHis').rand(001,999);
                $data['match_id']=$match_id;
                $data['seller_id']=M('Supply')->where('supply_id='.$post['supply_id'])->getField('member_id');
                $data['buyer_id']=M('Purchase')->where('purchase_id='.$post['purchase_id'])->getField('member_id');
                $data['order_amount']=$post['goods_amount'];
                $data['order_type']=2;  //1为商品订单2为撮合订单
                $data['order_state_id']=1;  //状态ID
                $data['order_time']=time();
                //print_r($data);exit;

                if(!$Morder->where('match_id='.$match_id)->getField('shipping_address')){
                    $data['shipping_address']=$Address->where("member_id=$member_id", "default=1")->getField('member_address');
                    $data['shipping_zipcode']=$Address->where("member_id=$member_id", "default=1")->getField('member_zipcode');
                    $data['shipping_truename']=$Address->where("member_id=$member_id", "default=1")->getField('member_truename');
                    $data['shipping_telephone']=$Address->where("member_id=$member_id", "default=1")->getField('member_telephone');
                }
                $Order=D("Order");
                $Order->data=$data;
                if(!$Order->check_add_order()){
                    echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
                }
            }
            echo "<script>alert('提交成功');window.location.href='/order/match';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //ajax价格变动
    public function match_add_ajax(){
        $post=I('post.');
//        $data=$post['purchase_id'];
        $Purchase=D("Purchase");
        $Supply=D("Supply");
        $Purchase->purchase_id=$post['purchase_id'];
        $Supply->supply_id=$post['supply_id'];
        $purchase_info=$Purchase->get_purchase_info();
        $supply_info=$Supply->get_supply_info();
        $data['price']=$purchase_info['purchase_price']>$supply_info['supply_price']?$supply_info['supply_price']:$purchase_info['purchase_price'];//采购与供应取价格低的那个
        $data['num']=$purchase_info['purchase_num']>$supply_info['supply_stock']?$supply_info['supply_stock']:$purchase_info['purchase_num'];//采购数量与供应库存取少的那个
        $data['num']=$data['num']>$supply_info['supply_mininum']?$data['num']:$supply_info['supply_mininum'];//数量不得低于起订量
        $data['unit_id']=$purchase_info['unit_id'];
        $data['msg']='ok';
        echo json_encode($data);
//        $this->assign("purchase_info",$purchase_info);
//        $this->assign("supply_info",$supply_info);
    }
    //
    public function supply(){
        $data['title']='供货列表 交易 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Supply");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $supply_list=$Model->get_supply_list();
        //print_r($order_list);exit;
        $supply_total=$Model->get_supply_total();
        //echo $user_total;exit;
        $page_total=ceil($supply_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['supply_total']=$supply_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("supply_list", $supply_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //更新排序及批量删除
    public function update_supply()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Supply");
            $Model->ids=$ids;
            if(!$Model->delete_supply()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.supply_ids')){
            $ids=I('post.supply_ids');
            $sorts=I('post.sorts');
            $Model=D("Supply");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_supply_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //
    public function supply_edit(){
        $data['title']='编辑供货信息 交易 - 找金银网';
        $supply_id=I('path.2');
        $Model=D("Supply");
        $Product=D("Product");  //产品
        $Member=D("Member");  //会员
        $Brand=D("Brand");  //品牌
        $Spec=D("Spec");  //规格
        $Unit=D("Unit");  //计量单位
        $Depot=D("Depot");  //仓库
        $DeliverTime=D("DeliverTime");  //发货时间
        $DeliverType=D("DeliverType");  //发货类型
        $ShippingType=D("ShippingType");  //物流方式
        $Model->supply_id=$supply_id;
        $supply_info=$Model->get_supply_info();
        $cat_list=$Product->get_cat_list();
        $member_list=$Member->get_member_list();
        $brand_list=$Brand->get_brand_list();
        $spec_list=$Spec->get_spec_list();
        $unit_list=$Unit->get_unit_list();
        $depot_list=$Depot->get_depot_list();
        $deliver_time_list=$DeliverTime->get_deliver_time_list();
        $deliver_type_list=$DeliverType->get_deliver_type_list();
        $shipping_type_list=$ShippingType->get_shipping_type_list();

        /*$SupplyState=D("SupplyState");  //采购状态
        $supply_state_list=$SupplyState->get_supply_state_list();
        $this->assign("supply_state_list", $supply_state_list);

        $Admin=D("Admin");  //后台用户
        $admin_list=$Admin->get_admin_list();
        $this->assign("admin_list", $admin_list);*/

        //print_r($order_info);exit;
        $this->assign("data", $data);
        $this->assign("supply_info", $supply_info);
        $this->assign("cat_list", $cat_list);
        $this->assign("member_list", $member_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("spec_list", $spec_list);
        $this->assign("unit_list", $unit_list);
        $this->assign("depot_list", $depot_list);
        $this->assign("deliver_time_list", $deliver_time_list);
        $this->assign("deliver_type_list", $deliver_type_list);
        $this->assign("shipping_type_list", $shipping_type_list);
        $this->display();
    }
    //
    public function supply_edit_post(){
        if($_POST['supply_title']!=''&&$_POST['supply_desc']!=''){
            $post=I('post.');
            $member_id=$post['member_id'];
            //echo $member_id;exit;
            if(!empty($_FILES['supply_image']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/supply/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $supply_image='/data/upload/supply/'.$member_id.'/'.$info['supply_image']['savename'];
                //echo $supply_image;exit;
                $post['supply_image']=$supply_image;
            }
            if(!empty($_FILES['supply_voucher']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/supply_voucher/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $supply_voucher='/data/upload/supply_voucher/'.$member_id.'/'.$info['supply_voucher']['savename'];
                //echo $supply_voucher;exit;
                $post['supply_voucher']=$supply_voucher;
            }
            $post['supply_desc']=I('post.supply_desc','','');
            //print_r($post);exit;
            $Model=D("Supply");
            $Model->post=$post;
            if(!$Model->supply_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/order/supply';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //
    public function supply_add(){
        $data['title']='添加供货信息 交易 - 找金银网';
        $Product=D("Product");  //产品
        $Member=D("Member");  //会员
        $Brand=D("Brand");  //品牌
        $Spec=D("Spec");  //规格
        $Unit=D("Unit");  //计量单位
        $Depot=D("Depot");  //仓库
        $DeliverTime=D("DeliverTime");  //发货时间
        $DeliverType=D("DeliverType");  //发货类型
        $ShippingType=D("ShippingType");  //物流方式
        $cat_list=$Product->get_cat_list();
        $member_list=$Member->get_member_list();
        $brand_list=$Brand->get_brand_list();
        $spec_list=$Spec->get_spec_list();
        $unit_list=$Unit->get_unit_list();
        $depot_list=$Depot->get_depot_list();
        $deliver_time_list=$DeliverTime->get_deliver_time_list();
        $deliver_type_list=$DeliverType->get_deliver_type_list();
        $shipping_type_list=$ShippingType->get_shipping_type_list();
        //print_r($unit_list);exit;
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("member_list", $member_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("spec_list", $spec_list);
        $this->assign("unit_list", $unit_list);
        $this->assign("depot_list", $depot_list);
        $this->assign("deliver_time_list", $deliver_time_list);
        $this->assign("deliver_type_list", $deliver_type_list);
        $this->assign("shipping_type_list", $shipping_type_list);
        $this->display();
    }
    //
    public function supply_add_post(){
        if($_POST['supply_title']!=''&&$_POST['supply_desc']!=''){
            $post=I('post.');
            $member_id=$post['member_id'];
            //echo $member_id;exit;
            if(!empty($_FILES['supply_image']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/supply/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $supply_image='/data/upload/supply/'.$member_id.'/'.$info['supply_image']['savename'];
                //echo $supply_image;exit;
                $post['supply_image']=$supply_image;
            }
            if(!empty($_FILES['supply_voucher']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/supply_voucher/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $supply_voucher='/data/upload/supply_voucher/'.$member_id.'/'.$info['supply_voucher']['savename'];
                //echo $supply_voucher;exit;
                $post['supply_voucher']=$supply_voucher;
            }
            $post['supply_desc']=I('post.supply_desc','','');
            $post['supply_time']=time();
            $post['supply_sn']=date('YmdHis',time()).rand(001,999);
            //print_r($post);exit;
            $Model=D("Supply");
            $Model->post=$post;
            if(!$Model->supply_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/order/supply';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //
    public function purchase(){
        $data['title']='采购列表 交易 - 找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Purchase");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $purchase_list=$Model->get_purchase_list();
        //print_r($order_list);exit;
        $purchase_total=$Model->get_purchase_total();
        //echo $user_total;exit;
        $page_total=ceil($purchase_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['purchase_total']=$purchase_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("purchase_list", $purchase_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //更新排序及批量删除
    public function update_purchase()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("Purchase");
            $Model->ids=$ids;
            if(!$Model->delete_purchase()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.purchase_ids')){
            $ids=I('post.purchase_ids');
            $sorts=I('post.sorts');
            $Model=D("Purchase");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_purchase_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //
    public function purchase_edit(){
        $data['title']='编辑求购信息 交易 - 找金银网';
        $purchase_id=I('path.2');
        $Model=D("Purchase");
        $Product=D("Product");  //产品
        $Member=D("Member");  //会员
        $Brand=D("Brand");  //品牌
        $Spec=D("Spec");  //规格
        $Unit=D("Unit");  //计量单位
        $Depot=D("Depot");  //仓库
        $DeliverTime=D("DeliverTime");  //发货时间
        $DeliverType=D("DeliverType");  //发货类型
        $ShippingType=D("ShippingType");  //物流方式
        $Model->purchase_id=$purchase_id;
        $purchase_info=$Model->get_purchase_info();
        $cat_list=$Product->get_cat_list();
        $member_list=$Member->get_member_list();
        $brand_list=$Brand->get_brand_list();
        $spec_list=$Spec->get_spec_list();
        $unit_list=$Unit->get_unit_list();
        $depot_list=$Depot->get_depot_list();
        $deliver_time_list=$DeliverTime->get_deliver_time_list();
        $deliver_type_list=$DeliverType->get_deliver_type_list();
        $shipping_type_list=$ShippingType->get_shipping_type_list();

        /*$PurchaseState=D("PurchaseState");  //采购状态
        $purchase_state_list=$PurchaseState->get_purchase_state_list();
        $this->assign("purchase_state_list", $purchase_state_list);

        $Admin=D("Admin");  //后台用户
        $admin_list=$Admin->get_admin_list();
        $this->assign("admin_list", $admin_list);*/
        //print_r($purchase_state_list);exit;
        $this->assign("data", $data);
        $this->assign("purchase_info", $purchase_info);
        $this->assign("cat_list", $cat_list);
        $this->assign("member_list", $member_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("spec_list", $spec_list);
        $this->assign("unit_list", $unit_list);
        $this->assign("depot_list", $depot_list);
        $this->assign("deliver_time_list", $deliver_time_list);
        $this->assign("deliver_type_list", $deliver_type_list);
        $this->assign("shipping_type_list", $shipping_type_list);
        $this->display();
    }
    //
    public function purchase_edit_post(){
        if($_POST['purchase_title']!=''&&$_POST['purchase_desc']!=''){
            $post=I('post.');
            $member_id=$post['member_id'];
            if(!empty($_FILES['purchase_image']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/purchase/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $purchase_image='/data/upload/purchase/'.$member_id.'/'.$info['purchase_image']['savename'];
                //echo $purchase_image;exit;
                $post['purchase_image']=$purchase_image;
            }
            if(!empty($_FILES['purchase_voucher']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/purchase_voucher/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $purchase_voucher='/data/upload/purchase_voucher/'.$member_id.'/'.$info['purchase_voucher']['savename'];
                //echo $purchase_voucher;exit;
                $post['purchase_voucher']=$purchase_voucher;
            }
            $post['purchase_desc']=I('post.purchase_desc','','');
            //print_r($post);exit;
            $Model=D("Purchase");
            $Model->post=$post;
            if(!$Model->purchase_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/order/purchase';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //
    public function purchase_add(){
        $data['title']='添加采购信息 交易 - 找金银网';
        $Product=D("Product");  //产品
        $Member=D("Member");  //会员
        $Brand=D("Brand");  //品牌
        $Spec=D("Spec");  //规格
        $Unit=D("Unit");  //计量单位
        $Depot=D("Depot");  //仓库
        $DeliverTime=D("DeliverTime");  //发货时间
        $DeliverType=D("DeliverType");  //发货类型
        $ShippingType=D("ShippingType");  //物流方式
        $PurchaseState=D("PurchaseState");  //采购状态
        $cat_list=$Product->get_cat_list();
        $member_list=$Member->get_member_list();
        $brand_list=$Brand->get_brand_list();
        $spec_list=$Spec->get_spec_list();
        $unit_list=$Unit->get_unit_list();
        $depot_list=$Depot->get_depot_list();
        $deliver_time_list=$DeliverTime->get_deliver_time_list();
        $deliver_type_list=$DeliverType->get_deliver_type_list();
        $shipping_type_list=$ShippingType->get_shipping_type_list();
        $purchase_state_list=$PurchaseState->get_purchase_state_list();
        $this->assign("data", $data);
        $this->assign("cat_list", $cat_list);
        $this->assign("member_list", $member_list);
        $this->assign("brand_list", $brand_list);
        $this->assign("spec_list", $spec_list);
        $this->assign("unit_list", $unit_list);
        $this->assign("depot_list", $depot_list);
        $this->assign("deliver_time_list", $deliver_time_list);
        $this->assign("deliver_type_list", $deliver_type_list);
        $this->assign("shipping_type_list", $shipping_type_list);
        $this->assign("purchase_state_list", $purchase_state_list);
        $this->display();
    }
    //
    public function purchase_add_post(){
        if($_POST['purchase_title']!=''&&$_POST['purchase_desc']!=''){
            $post=I('post.');
            $member_id=$post['member_id'];
            //echo $member_id;exit;
            if(!empty($_FILES['purchase_image']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/purchase/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $purchase_image='/data/upload/purchase/'.$member_id.'/'.$info['purchase_image']['savename'];
                //echo $purchase_image;exit;
                $post['purchase_image']=$purchase_image;
            }
            if(!empty($_FILES['purchase_voucher']['tmp_name'])){
                $root_path=ROOT_PATH.'www/data/upload/purchase_voucher/'.$member_id;
                //echo $root_path;exit;
                if(!is_dir($root_path)){
                    mkdir($root_path);
                }
                //上传图片
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize=2048000;// 设置附件上传大小
                $upload->exts=array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
                $upload->savePath=''; // 设置附件上传（子）目录
                $upload->subName='';
                $upload->saveName=md5(time());
                $info=$upload->upload();
                if(!$info) {// 上传失败
                    echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
                }
                $purchase_voucher='/data/upload/purchase_voucher/'.$member_id.'/'.$info['purchase_voucher']['savename'];
                //echo $purchase_voucher;exit;
                $post['purchase_voucher']=$purchase_voucher;
            }
            $post['purchase_desc']=I('post.purchase_desc','','');
            $post['purchase_time']=time();
            $post['purchase_sn']=date('YmdHis',time()).rand(001,999);
            //print_r($post);exit;
            $Model=D("Purchase");
            $Model->post=$post;
            if(!$Model->purchase_add_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>alert('提交成功');window.location.href='/order/purchase';</script>";
        }else{
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
    }
    //
    public function quotation(){
        $data['title']='资源单管理_交易_找金银网';
        if(I('path.2')!=''){
            $page=I('path.2');
        }else{
            $page=1;
        }
        $perpage=10;
        $Model=D("Quotation");
        //搜索 关键词
        if(I('post.keyword')){
            $Model->keyword=I('post.keyword');
        }
        //搜索 分类
        /*if(I('post.pc_id')){
            $Model->pc_id=I('post.pc_id');
        }*/
        $Model->page=$page;
        $Model->perpage=$perpage;
        $quotation_list=$Model->get_quotation_list();
        //print_r($quotation_list);exit;
        $quotation_total=$Model->get_quotation_total();
        //echo $user_total;exit;
        $page_total=ceil($quotation_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['quotation_total']=$quotation_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("quotation_list", $quotation_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->display();
    }
    //更新排序及批量删除
    public function update_quotation()
    {
        if(I('post.ids')){
            $ids=I('post.ids');
            $Model=D("quotation");
            $Model->ids=$ids;
            if(!$Model->delete_quotation()){
                echo "<script>alert('删除失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";exit;
        }elseif(I('post.quotation_ids')){
            $ids=I('post.quotation_ids');
            $sorts=I('post.sorts');
            $Model=D("quotation");
            $Model->ids=$ids;
            $Model->sorts=$sorts;
            if(!$Model->update_quotation_sort()){
                echo "<script>alert('更新失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            echo "<script>self.location=document.referrer;</script>";
        }
    }
    //
    public function quotation_edit(){
        $data['title']='编辑供货信息 交易 - 找金银网';
        $quotation_id=I('path.2');
        $Model=D("quotation");
        $Member=D("Member");  //会员
        $Model->quotation_id=$quotation_id;
        $quotation_info=$Model->get_quotation_info();
        //print_r($quotation_info);exit;
        $member_list=$Member->get_member_list();
        //print_r($order_info);exit;
        $this->assign("data", $data);
        $this->assign("quotation_info", $quotation_info);
        $this->assign("member_list", $member_list);
        $this->display();
    }
    //
    public function quotation_edit_post(){
        $post=I('post.');
        $member_id=$post['member_id'];
        //echo $member_id;exit;
        if(!empty($_FILES['quotation_file']['tmp_name'])){
            $root_path=ROOT_PATH.'www/data/upload/quotation/'.$member_id;
            //echo $root_path;exit;
            if(!is_dir($root_path)){
                mkdir($root_path);
            }
            //echo $root_path;exit;
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize=2048000;// 设置附件上传大小 2M
            $upload->exts=array('xls', 'xlsx');// 设置附件上传类型
            $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
            $upload->savePath=''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName=md5(time());
            $info=$upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $post['quotation_file']='/data/upload/quotation/'.$member_id.'/'.$info['quotation_file']['savename'];
        }
        //print_r($post);exit;
        $Model=D("quotation");
        $Model->post=$post;
        if(!$Model->quotation_edit_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/order/quotation';</script>";
    }
    //
    public function quotation_add(){
        $data['title']='添加资源单_交易_找金银网';
        $Member=D("Member");  //会员
        $member_list=$Member->get_member_list();
        $this->assign("data", $data);
        $this->assign("member_list", $member_list);
        $this->display();
    }
    //
    public function quotation_add_post(){
        if(empty($_FILES['quotation_file']['tmp_name'])){
            echo "<script>alert('请您填写完整后再尝试重新提交');history.back(-1);</script>";exit;
        }
        $post=I('post.');
        $member_id=$post['member_id'];
        //echo $member_id;exit;
        $root_path=ROOT_PATH.'www/data/upload/quotation/'.$member_id;
        //echo $root_path;exit;
        if(!is_dir($root_path)){
            mkdir($root_path);
        }
        //上传图片
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize=2048000;// 设置附件上传大小 2M
        $upload->exts=array('xls', 'xlsx');// 设置附件上传类型
        $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
        $upload->savePath=''; // 设置附件上传（子）目录
        $upload->subName='';
        $upload->saveName=md5(time());
        $info=$upload->upload();
        if(!$info) {// 上传失败
            echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
        }
        $quotation_file='/data/upload/quotation/'.$member_id.'/'.$info['quotation_file']['savename'];
        //echo $quotation_file;exit;
        $post['quotation_file']=$quotation_file;
        $post['quotation_sn']=date('YmdHis',time()).rand(001,999);
        $post['quotation_add_time']=time();
        //print_r($post);exit;
        $Model=D("Quotation");
        $Model->post=$post;
        if(!$Model->quotation_add_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/order/quotation';</script>";
    }
    //
    public function virtual(){
        $data['title']='虚拟订单 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function invoice(){
        $data['title']='发票管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function refund(){
        $data['title']='退款管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function returngoods(){
        $data['title']='退货管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function consultation(){
        $data['title']='咨询管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function report(){
        $data['title']='举报管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function evaluate(){
        $data['title']='评价管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //
    public function complaint(){
        $data['title']='投诉管理 交易 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    //意向
    public function intention(){
        $data['title']='意向管理 交易 找金银网';
        $Model=D("Intention");
        $Model->member_id=$this->member_id;
        // $Model->intention_type=1;
        $intention_list=$Model->get_intention_list();
        // dump($intention_list);exit;
        $this->assign("intention_list",$intention_list);
        $this->assign("data", $data);
        $this->display();
    }
    //意向编辑
    public function intention_edit($id){
        $data['title']='意向管理 交易 找金银网';
        $Model=D("Intention");
        $purchase_id=$Model->where('intention_id='.$id)->getField('purchase_id');
        $supply_id=$Model->where('intention_id='.$id)->getField('supply_id');
        $Purchase=D("Purchase");
        $Model->purchase_title=$Purchase->where('purchase_id='.$purchase_id)->getField('purchase_title');
        $Supply=D("Supply");
        $Model->supply_title=$Supply->where('supply_id='.$supply_id)->getField('supply_title');
        $Model->member_id=$this->member_id;
        $Model->intention_id=$id;
        // var_dump($Model->supply_title);exit;
        // $Model->intention_type=1;
        $intention_info=$Model->get_intention_info();
        // $match_id=$Model->where('intention_id='.$id)->getField('match_id');
        // $Match=D("Match");

        $this->assign("intention_info",$intention_info);
        $this->assign("data", $data);
        $this->display();
    }
    //意向编辑提交
    public function intention_edit_post(){
        $Model=D("Intention");
        $Match=D("Match");
        $post=I('post.');
        
        if(!$Model->intention_edit_post()){
                echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
            }
            $post['match_state_id']=$post['intention_state'];
            $Match->post=$post;
            // $match_state_id=$Match->where('match_id='.$post['match_id'])->getField('match_state_id');
            // $post['match_state_id']=$match_state_id;
            if(!$Match->match_edit_post()){
                    echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
                }
            echo "<script>alert('提交成功');window.location.href='/order/intention';</script>";
        
    }
    //首页留言
    public function message(){
        $data['title']='首页留言 - 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
}